package abd.pr1.Controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

import abd.pr1.Interface.NuevaSerieWindow;
import abd.pr1.Interface.NuevoActorWindow;
import abd.pr1.Interface.NuevoPersonajeWindow;
import abd.pr1.Mappers.ActoresMapper;
import abd.pr1.Mappers.GenerosMapper;
import abd.pr1.Mappers.PersonajesActoresMapper;
import abd.pr1.Mappers.PersonajesMapper;
import abd.pr1.Mappers.SeriesGenerosMapper;
import abd.pr1.Mappers.SeriesMapper;
import abd.pr1.Models.Actor;
import abd.pr1.Models.Genero;
import abd.pr1.Models.Personaje;
import abd.pr1.Models.PersonajeActor;
import abd.pr1.Models.Serie;
import abd.pr1.Models.SerieGenero;

public class NuevoActorWindowController  implements ActionListener{
	private NuevoActorWindow nuevoActorWindow;
	private DataSource ds;
	private Actor actor;
	private AdminActoresPanelController adminActoresPanelController;
	
	public NuevoActorWindowController( AdminActoresPanelController adminActoresPanelController,NuevoActorWindow nuevoActorWindow,DataSource ds){
		this.nuevoActorWindow=nuevoActorWindow;
		this.ds=ds;
		this.nuevoActorWindow.addListener(this);
		actor= new Actor(null,null,null,null);
		this.adminActoresPanelController=adminActoresPanelController;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("seleccion_foto")){
			
			JFileChooser fc = this.nuevoActorWindow.getFileChooser();
			FileNameExtensionFilter filtroImagen=new FileNameExtensionFilter("JPG|PNG|GIF","jpg","png","gif");
			fc.setAcceptAllFileFilterUsed(false);
			fc.setFileFilter(filtroImagen);
			
			int returnVal = fc.showOpenDialog(this.nuevoActorWindow);
			
			if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = fc.getSelectedFile();
	            byte[] b = new byte[(int) file.length()];

	            if(file.length()<65*1024){
	            	try {
		                  FileInputStream fileInputStream = new FileInputStream(file);
		                  fileInputStream.read(b);
		                  
		                  this.actor.setFoto(b);
		                  this.nuevoActorWindow.setImagen(new ImageIcon(b));
		                  
		                  fileInputStream.close();
		                  
		             } catch (FileNotFoundException e1) {
		                         System.out.println("File Not Found.");
		                         e1.printStackTrace();
		             }
		             catch (IOException e1) {
		                      System.out.println("Error Reading The File.");
		                       e1.printStackTrace();
		             }
	            } else {
	            	JOptionPane.showMessageDialog(null, "Imagen demasiado grande. Tiene que ser menor de 64 KB", "Error en imagen", JOptionPane.ERROR_MESSAGE);
	            }
	            
	            
	        }
			
		}
		
		
		if(e.getActionCommand().equals("guardar_actor")){
			String nombreActores=this.nuevoActorWindow.getNombreTexto();
			Date nacimiento;
			nacimiento = this.nuevoActorWindow.getNacimiento();
			actor.setNombre(nombreActores);
			actor.setNacimiento(nacimiento);
			
			if(actor.getFoto()==null){
				File imagendefecto= new File("src/abd/pr1/Interface/silueta.gif");
				byte[] b = new byte[(int) imagendefecto.length()];
				
				FileInputStream fileInputStream;
				try {
					fileInputStream = new FileInputStream(imagendefecto);
					fileInputStream.read(b);
					fileInputStream.close();
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				
				actor.setFoto(b);
			}
			
			new ActoresMapper(ds).insert(actor);
			this.adminActoresPanelController.refreshActores();
			nuevoActorWindow.dispose();
			
		} else if(e.getActionCommand().equals("cerrar_actor")){
			nuevoActorWindow.dispose();
		}
	}
}
